<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/10
 * Time: 8:48
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class RoleModel
{
    const TABLE = 'role';

    /**
     * 角色列表
     * @param array $param
     * @return mixed
     */
    public function lists(array $param)
    {
        $limit = config('yucheng.limit');
        $start = 0;
        if (isset($param['length']) && !is_null($param['length'])) {
            $limit = $param['length'];
        }
        if (isset($param['page']) && !is_null($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }

        return DB::table(self::TABLE)
            ->where(function ($query) use ($param) {
                if (!empty($param['isProject'])) {
                    $query->where('isProject', $param['isProject']);
                }
                if (!empty($param['search'])) {
                    $query->where('name', 'like', '%' . $param['search'] . '%');
                }
            })
            ->offset($start)->limit($limit)
            ->get()->toArray();
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function countLists(array $param)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($param) {
                if (isset($param['isProject']) && !is_null($param['isProject'])) {
                    $query->where('isProject', $param['isProject']);
                }
                if (isset($param['search']) && !is_null($param['search'])) {
                    $query->where('name', 'like', '%' . $param['search'] . '%');
                }
            })->count();
    }

    /**
     * @param array $array
     * @param int $id
     * @return array
     */
    public function checkRepeat(array $array, $id = 0)
    {
        $result = DB::table(self::TABLE)->where($array)
            ->where(function ($query) use ($id) {
                if ($id != 0) {
                    $query->where('id', '!=', $id);
                }
            })
            ->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        return DB::table(self::TABLE)->insertGetId($data);
    }

    /**
     * @param array $data
     * @return array
     */
    public function info($data = [])
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * @param array $data
     * @return bool
     */
    public function delete(array $data)
    {
        try{
            DB::transaction(function () use ($data) {
                DB::table(self::TABLE)->where($data)->delete();
                (new RolePermissionModel())->delete(['roleId'=>$data['id']]);
            });
            return true;
        }catch(\Exception $e){
            return false;
        }
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function selectLists(array $data){
        return DB::table(self::TABLE)
            ->where(function($query) use ($data){
                if (!empty($param['projectId'])){
                    if ($param['projectId'] <= 1){
                        $isProject = 1;
                    }else{
                        $isProject = 2;
                    }
                    $query->where('isProject',$isProject);
                }
            })
            ->get()->toArray();
    }
}